const util = require('../../utils/util.js');
const api = require('../../config/api.js');
const user = require('../../utils/user.js');
const dianpu = require('../../utils/common/dianpu.js');
//获取应用实例
const app = getApp();

Page({
  data: {
    goods: [],
    guanggao: [],
    channel: [],
    notice: [],
    goodsCount: 0,
    indicatorDots: false,
    appid: "",
    page: 1,
    limit: 10
  },

  getIndexData: function () {
    let that = this;
    util.request(api.HomeIndex, {
      appid: that.data.appid,
      page: that.data.page,
      limit: that.data.limit
    }).then(function (res) {
      if (res.result == 1) {
        that.setData({
          goods: res.data.goods,
          guanggao: res.data.guanggao,
          notice: res.data.notice,
          channel: res.data.channel
        });
      }
    });
  },

  getHomeGoods: function () {
    let that = this;
    util.request(api.HomeGoodsList, {
      page: that.data.page + 1,
      limit: that.data.limit
    }).then(function (res) {
      if (res.result == 1) {
        if (res.data.length > 0) {
          that.setData({
            goods: that.data.goods.concat(res.data),
            page: that.data.page + 1
          });
        } else {
          wx.showToast({
            title: '已经被您看完了!',
            icon: 'none',
            duration: 2000
          });
          return false;
        }
      }
    });
  },
  //点击更多跳转分类
  openCartPage: function () {
    wx.switchTab({
      url: '/pages/fenlei/fenlei'
    });
  },
  //点击电话图标
  tel: function () {
    dianpu.getmendiantel().then(res => {
      wx.makePhoneCall({
        phoneNumber: res.lianxirenmobile,
      })
    }).catch(() => {
      wx.showToast({
        title: '获取联系方式失败!',
        icon: 'none',
        duration: 2000
      });
    });
  },
  onLoad: function (options) {
    //如果有分享用户，则设置
    let that = this;
    var app = getApp();
    var appid = app.globalData.appid;
    that.setData({
      appid: appid
    });
    // 页面初始化 options为页面跳转所带来的参数
    if (options.scene) {
      //这个scene的值存在则证明首页的开启来源于朋友圈分享的图,同时可以通过获取到的goodId的值跳转导航到对应的详情页
      var scene = decodeURIComponent(options.scene);
      util.request(api.ShibieQcodeImg, {
        id: scene
      }, 'GET').then(function (res) {
        if (res.result == 1) {
          var page = res.data.page;
          var page_data_id = res.data.page_data_id;
          var othersign = res.data.othersign;
          //普通商品分享
          if (page == 'goods') {
            wx.navigateTo({
              url: '../goods/goods?id=' + page_data_id
            });
          }
          //团购活动分享
          else if (page == 'groupbuyingDetail') {
            wx.navigateTo({
              url: '../marketing/groupbuying/groupbuyingDetail/groupbuyingDetail?id=' + page_data_id
            });
          }
          //秒杀活动分享
          else if (page == 'seckillDetail') {
            wx.navigateTo({
              url: '../marketing/seckill/seckillDetail/seckillDetail?id=' + page_data_id
            });
          }
          //拼团活动分享
          else if (page == 'pintuanDetail') {
            /**
             * othersign参数是表file_qcode生成海报二维码的表中的标记字段，这里如果值为“isyaoqing”则表示该拼团活动已开团
              如果已开团的则需要带上isyaoqing参数，后面其他会员通过
              点击分享页面进来时会根据该参数判断是否已开团，如果已开团则不再显示拼团（开团）按钮，而是显示邀请，或者参与拼团按钮
            */
            if (othersign == 'isyaoqing') {
              //此值memberUserid是分享会员的id
              let shareMemberUserid = res.data.memberUserid;
              //带上isyaoqing参数跳转至拼团详情页时会根据此参数判断是否已开团，来显示按钮；此值为固定值，不能修改
              wx.navigateTo({
                url: '/pages/marketing/pintuan/pintuanDetail/pintuanDetail?id=' + page_data_id + '&shareUserid=' + shareMemberUserid + '&isyaoqing=isyaoqing'
              });
            } else {
              wx.navigateTo({
                url: '/pages/marketing/pintuan/pintuanDetail/pintuanDetail?id=' + page_data_id
              });
            }
          }
          //分销商推广整站
          if (page == 'home') {
            //不作处理
          }
        }
      });
    }
    if (options.shareMemberUserid) {
      //这个shareMemberUserid的存在证明是通过分享页面进来的
      util.request(api.Yemianfenxiang, {
        shareMemberUserid: options.shareMemberUserid,
        page_data_id: options.id,
        page: options.page
      }, 'GET').then(function (res) {
        if (res.result == 1) {
          console.log(options.shareMemberUserid);
        }
      });
    }
    that.getIndexData();
  },
  onReady: function () {},

  onShow: function () {
    var _cartnum = wx.getStorageSync('cartnum') + '';
    if (_cartnum) {
      wx.setTabBarBadge({
        index: 2,
        text: _cartnum
      })
    }
  },

  // 页面分享
  onShareAppMessage: function () {
    return {
      title: '首页',
      desc: '好东西要一起分享！',
      path: '/pages/home/home'
    }
  },

  onHide: function () {
    // 页面隐藏
  },

  onUnload: function () {
    // 页面关闭
  },
  // 下拉刷新
  onReachBottom() {
    wx.showNavigationBarLoading() //在标题栏中显示加载
    this.getHomeGoods();
    wx.hideNavigationBarLoading() //完成停止加载
    wx.stopPullDownRefresh() //停止下拉刷新
  }
})